package com.csl.jsonpreference.ui


import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import androidx.recyclerview.widget.LinearLayoutManager
import com.csl.jsonpreference.databinding.DialogItemTipBinding
import com.csl.ui.base.CenterDialog
import com.csl.ui.base.adapter.QuickAdapter


/**
 * 提示对话框
 */
open class ListChoseDialog<T> : CenterDialog {

    var title: String = ""
    var content: String? = null
    var items: ArrayList<T> = ArrayList()
    var listener: ((T) -> Unit)? = null

    constructor(
        title: String,
        content: String? = null,
        vararg items: T,
        listener: (T) -> Unit
    ) : super() {
        this.title = title
        this.content = content
        this.items.addAll(items)
        this.listener = listener
    }

    constructor(
        title: String,
        content: String? = null,
        items: List<T>? = null,
        listener: (T) -> Unit
    ) : super() {
        this.title = title
        this.content = content
        items?.let {
            this.items.addAll(items)
        }
        this.listener = listener
    }
//    constructor(
//        title: String,
//        content: String? = null,
//        items: Array<T>?,
//        listener: (T) -> Unit
//    ) : super() {
//        this.title = title
//        this.content = content
//        items?.let {
//            this.items.addAll(items)
//        }
//        this.listener = listener
//    }

    lateinit var binding: DialogItemTipBinding

    var itemAdapter: TextAdapter<T> = TextAdapter<T>().apply {
        onItemClickListener = object : QuickAdapter.OnItemSelectListener<T> {
            override fun onItemSelect(index: Int, data: T) {
                listener?.invoke(data)
                dismiss()
            }
        }
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        getDialog()?.requestWindowFeature(Window.FEATURE_NO_TITLE); // 不显示标题栏
        binding = DialogItemTipBinding.inflate(inflater, container, false)

        initListener()


//        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE) // 不显示标题栏
        //        startUpAnimation(dialogView);
        return binding.root
    }

    override fun onStart() {
        super.onStart()
        initData()
    }

    private fun initListener() {}

    private fun initData() {

        binding.jsonTvTitle.text = title
        if (TextUtils.isEmpty(content)) {
            binding.jsonEtContent.visibility = View.GONE
        } else {
            binding.jsonEtContent.setText("$content")
            binding.jsonEtContent.visibility = View.VISIBLE
        }


        binding.jsonBtnCancel.setOnClickListener {
            dismiss()
        }
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)

        binding.llDialogmain.setOnClickListener {}//有用

        binding.rvItems.layoutManager = LinearLayoutManager(context).apply {
            orientation = LinearLayoutManager.VERTICAL
        }
        binding.rvItems.adapter = itemAdapter
        itemAdapter.resetData(
            items.toList()
        )
    }

}